package com.hjjt.cameralib.fragment

import android.Manifest
import android.os.Bundle
import androidx.activity.result.contract.ActivityResultContracts
import androidx.fragment.app.Fragment

/**
 *
 * @ProjectName:    CameraLib
 * @Package:        com.hjjt.cameralib.fragment
 * @ClassName:      PermissionFragment
 * @Description:
 * @Author:         孙浩
 * @CreateDate:     2022/6/2 15:08
 */
class PermissionFragment(val callBack: (allGranted: Boolean, grantedList: List<String>, deniedList: List<String>) -> Unit) :
    Fragment() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) {
            var allGranted = true
            val grantedList = mutableListOf<String>()
            val deniedList = mutableListOf<String>()
            it.forEach {
                if(it.value){
                    grantedList.add(it.key)
                }else{
                    allGranted = false
                    deniedList.add(it.key)
                }
            }
            callBack.invoke(allGranted,grantedList,deniedList)
        }.launch(
            arrayOf(
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.CAMERA,
            )
        )
    }

    fun requestPermission() {

    }
}